package paperockscissors;

public class RockPaperScissors {

	public Result play(RPSEnum p1, RPSEnum p2) {
		Result r = Result.LOST;

		switch (p1) {
		case PAPER:
			switch (p2) {
			case PAPER:
				r = Result.TIE;
				break;
			case ROCK:
				r = Result.WIN;
				break;
			case SCISSORS:
				r = Result.LOST;
				break;
			}
			break;
		case ROCK:
			switch (p2) {
			case ROCK:
				r = Result.TIE;
				break;
			case SCISSORS:
				r = Result.WIN;
				break;
			case PAPER:
				r = Result.LOST;
				break;
			}
			break;
		case SCISSORS:
			switch (p2) {
			case SCISSORS:
				r = Result.TIE;
				break;
			case PAPER:
				r = Result.WIN;
				break;
			case ROCK:
				r = Result.LOST;
				break;
			}
			break;
		}
		return r;
	}
	
	public Result play(Player p1, Player p2){
		// Compare the moves number of each player
		int nbMoves = 0;
		Result r;
		Result final_result;
		p1.setScore(0);
		p2.setScore(0);
		p1.resetMovesCount();
		p2.resetMovesCount();
		
		if(p1.getNbMoves()<=p2.getNbMoves())	nbMoves = p1.getNbMoves();
		else									nbMoves = p2.getNbMoves();
		
		for (int i=0; i<nbMoves; i++){
			r = play(p1.getNextMove(), p2.getNextMove());
			switch (r){
				case WIN:	p1.setScore(p1.getScore()+1);
							break;
							
				case TIE:	p1.setScore(p1.getScore()+1);
							p2.setScore(p2.getScore()+1);
							break;
							
				case LOST:	p2.setScore(p2.getScore()+1);
							break;
			}
		}
		if (p1.getScore() < p2.getScore()) final_result = Result.LOST;
		else if (p1.getScore() > p2.getScore()) final_result = Result.WIN;
		else final_result = Result.TIE;
		return final_result;
	}
}
